package com.cmc.cloud.cmclink.doc.service.impl;

import com.cmc.cloud.cmclink.doc.constants.errorconstant.ContainerMaxPayloadErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.ContainerMaxPayloadConvert;
import com.cmc.cloud.cmclink.doc.entity.ContainerMaxPayloadDO;
import com.cmc.cloud.cmclink.doc.mapper.ContainerMaxPayloadMapper;
import com.cmc.cloud.cmclink.doc.service.ContainerMaxPayloadService;
import com.cmc.cloud.cmclink.doc.vo.special.containermaxpayloadvo.ContainerMaxPayloadBaseVO;
import com.cmc.cloud.cmclink.doc.vo.special.containermaxpayloadvo.ContainerMaxPayloadCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.special.containermaxpayloadvo.ContainerMaxPayloadPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.special.containermaxpayloadvo.ContainerMaxPayloadUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

/**
 * 箱型最大载重维护 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class ContainerMaxPayloadServiceImpl implements ContainerMaxPayloadService {

    @Resource
    private ContainerMaxPayloadMapper containerMaxPayloadMapper;

    @Override
    public void createContainerMaxPayload(ContainerMaxPayloadCreateReqVO createReqVO) {
        if (null == createReqVO.getMaxPayload() && null == createReqVO.getLength() && null == createReqVO.getWidth() && null == createReqVO.getHeight()
                && null == createReqVO.getOverForward() && null == createReqVO.getOverAfter() && null == createReqVO.getOverLeft()
                && null == createReqVO.getOverRight() && null == createReqVO.getOverHeight()) {
            throw ServiceExceptionUtil.exception(ContainerMaxPayloadErrorCodeConstants.CONTAINER_ARG_ERROR);
        }
        // 检验唯一性
        validateUnique(createReqVO, null);

        // 插入
        ContainerMaxPayloadDO containerMaxPayload = ContainerMaxPayloadConvert.INSTANCE.convert(createReqVO);
        containerMaxPayloadMapper.insert(containerMaxPayload);
    }

    private void validateUnique(ContainerMaxPayloadBaseVO vo, Long id) {
        LambdaQueryWrapperX<ContainerMaxPayloadDO> queryWrapperX = new LambdaQueryWrapperX<ContainerMaxPayloadDO>()
                .eqIfPresent(ContainerMaxPayloadDO::getCntrType, vo.getCntrType())
                .eqIfPresent(ContainerMaxPayloadDO::getCntrSize, vo.getCntrSize());
        if (id != null) {
            queryWrapperX.ne(ContainerMaxPayloadDO::getId, id);
        }
        if (containerMaxPayloadMapper.selectCount(queryWrapperX) != 0L) {
            throw ServiceExceptionUtil.exception(ContainerMaxPayloadErrorCodeConstants.CONTAINER_LIMIT_EXISTED);
        }
    }

    @Override
    public void updateContainerMaxPayload(ContainerMaxPayloadUpdateReqVO updateReqVO) {
        // 校验存在
        validateContainerMaxPayloadExists(updateReqVO.getId());

        // 检验唯一性
        validateUnique(updateReqVO, updateReqVO.getId());

        // 更新
        ContainerMaxPayloadDO updateObj = ContainerMaxPayloadConvert.INSTANCE.convert(updateReqVO);
        containerMaxPayloadMapper.updateById(updateObj);
    }

    @Override
    public void deleteContainerMaxPayload(Long id) {
        // 校验存在
        validateContainerMaxPayloadExists(id);
        // 删除
        containerMaxPayloadMapper.deleteById(id);
    }

    private void validateContainerMaxPayloadExists(Long id) {
        if (containerMaxPayloadMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(ContainerMaxPayloadErrorCodeConstants.CONTAINER_NOT_EXISTED);
        }
    }

    @Override
    public ContainerMaxPayloadDO getContainerMaxPayload(Long id) {
        return containerMaxPayloadMapper.selectById(id);
    }

    @Override
    public PageResult<ContainerMaxPayloadDO> getContainerMaxPayloadPage(ContainerMaxPayloadPageReqVO reqVO) {
        LambdaQueryWrapperX<ContainerMaxPayloadDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eqIfPresent(ContainerMaxPayloadDO::getCntrType, reqVO.getCntrType())
                .eqIfPresent(ContainerMaxPayloadDO::getCntrSize, reqVO.getCntrSize());
        return containerMaxPayloadMapper.selectPage(reqVO, queryWrapperX);
    }
}
